require "BagTableLayer"
require "HeroEquipLayer"

ZBQHLayer = {}
ZBQHLayerFunc = {}
ZBQHLayerFunc.__index = ZBQHLayerFunc
setmetatable(ZBQHLayer, ZBQHLayerFunc)
ZBQHLayer.NodeID = {Menu=1001, TableBK=1002, EquipBK=1003, QiangHuaButton=1004, Item1=2001, Item2=2002}

ZBQHLayerFunc.getNode = function()
  local l = {}
  setmetatable(l, ZBQHLayerFunc)
  l.node = CCBReader:load("ZhuangBeiLayer0.ccbi")
  l:init()
  ZBQHLayer.instance = l
  return l
end
ZBQHLayerFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(ZBQHLayer:getNode().node)
  print("ZBQHLayer.getScene", sc)
  return sc
end

ZBQHLayerFunc.init = function(self)
  --[[
  local menu = self.node:getChildByTag(ZBQHLayer.NodeID.Menu)
  print("ZBQHLayerFunc.init node", self.node, menu)
  self.item1 = menu:getChildByTag(ZBQHLayer.NodeID.Item1)
  setObjectExtTable(self.item1, self);
  self.item2 = menu:getChildByTag(ZBQHLayer.NodeID.Item2)
  setObjectExtTable(self.item2, self);
  ]]
  local menu = self.node:getChildByTag(ZBQHLayer.NodeID.Menu)
  self.radioMenu = RadioMenu:create(menu, "menuItem1Changed", self)
  
  print("item", self.item1, self.item2)
  local button = self.node:getChildByTag(ZBQHLayer.NodeID.QiangHuaButton)
  setObjectExtTable(button, self);
  TypeCast(button, "CCButton"):setEnabled(false)
  --self:createBagTable()
  --self:createHeroEquipLayer()
  self.radioMenu:setMenuState(0)
end

ZBQHLayerFunc.createBagTable = function(self)
  local tb = self.node:getChildByTag(ZBQHLayer.NodeID.TableBK)
  return BagTable:create(TypeCast(tb, "CCSprite"), self, "onClickEquip", 1) 
  --self.node:addChild(self.bagTable.node)
end

ZBQHLayerFunc.createHeroEquipLayer = function(self)
  return HeroEquipLayer:create(self, "onClickEquip")
  --self.node:addChild(self.heroEquipLayer.node)
end

ZBQHLayerFunc.clearEquipInfo = function(self)
  if self.curEquipSprite~=nil then
    self.curEquipSprite:getParent():removeChild(self.curEquipSprite, true)
    self.curEquipSprite = nil
  end
  self.currentEquipIndex = nil
  local button = self.node:getChildByTag(ZBQHLayer.NodeID.QiangHuaButton)
  TypeCast(button, "CCButton"):setEnabled(false)

  TypeCast(self.node:getChildByTag(1009), "CCLabelTTF"):setString("")
  TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString("")
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString("")
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setString("")
  TypeCast(self.node:getChildByTag(1013), "CCLabelTTF"):setString("")
  TypeCast(self.node:getChildByTag(1014), "CCLabelTTF"):setString("")
  TypeCast(self.node:getChildByTag(1015), "CCLabelTTF"):setString("")
  TypeCast(self.node:getChildByTag(1016), "CCLabelTTF"):setString("")
  TypeCast(self.node:getChildByTag(1017), "CCLabelTTF"):setString("")
end
ZBQHLayerFunc.setEquipInfo = function(self, equipIndex)
  local equip = Equip:create(equipIndex)
  
  --if self.curEquipSprite~=nil then
  --  self.curEquipSprite:getParent():removeChild(self.curEquipSprite, true)
  --end
  self:clearEquipInfo()
  
  local button = self.node:getChildByTag(ZBQHLayer.NodeID.QiangHuaButton)
  TypeCast(button, "CCButton"):setEnabled(true)
  self.curEquipSprite = addSpriteFromBK(self.node, ZBQHLayer.NodeID.EquipBK, nameFromPath(equip.equipConfig.asset))
  self.currentEquipIndex = equipIndex 
  
  TypeCast(self.node:getChildByTag(1009), "CCLabelTTF"):setString(equip.equipConfig.name)
  TypeCast(self.node:getChildByTag(1009), "CCLabelTTF"):setColor(Equip.EquipColor[equip.equipConfig.quality])

  TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString("强化等级")
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("%d", equip.equipInfo.l))

  local key, desc, attr = equip:getAttr1()
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setString(desc)
  TypeCast(self.node:getChildByTag(1013), "CCLabelTTF"):setString(string.format("+%d", attr))
  
  key, desc, attr = equip:getAttr2()
  if desc~=nil then
    TypeCast(self.node:getChildByTag(1014), "CCLabelTTF"):setString(desc)
    TypeCast(self.node:getChildByTag(1015), "CCLabelTTF"):setString(string.format("+%d", attr))
  else
    TypeCast(self.node:getChildByTag(1014), "CCLabelTTF"):setString("")
    TypeCast(self.node:getChildByTag(1015), "CCLabelTTF"):setString("")
  end

  local rate = equip:getStrenthenSuccessRate()
  TypeCast(self.node:getChildByTag(1016), "CCLabelTTF"):setString(string.format("%d%%", rate*100))
  
  local cash = equip:getStrenthenCash()
  TypeCast(self.node:getChildByTag(1017), "CCLabelTTF"):setString(string.format("%d现金", cash))
end
ZBQHLayerFunc.menuItem1Changed = function(self, oldIndex, newIndex)
  --self.radioMenu1:setMenuState(newIndex)
  if self.contentLayer~=nil then
    self.node:removeChild(self.contentLayer.node, true)
  end
  self:clearEquipInfo()
  --self.bagOrHero = newIndex
  self.currentSelect = index
  if newIndex==0 then
    self.contentLayer = self:createHeroEquipLayer()
  else
    self.contentLayer = self:createBagTable()
  end
  self.node:addChild(self.contentLayer.node)
  --self.currentSelect = index
end

--[[
ZBQHLayerFunc.setMenuFocus = function(self, index)
  --self.currentSelect = index
  print("item", self.item1, self.item2)
  TypeCast(self.item1, "CCMenuItem"):unselected()
  TypeCast(self.item2, "CCMenuItem"):unselected()
  if index==0 then
    self.item1:selected()
  else
    self.item2:selected()
  end

  if index==self.currentSelect then return;end
  if self.contentLayer~=nil then
    self.node:removeChild(self.contentLayer.node, true)
  end
  if index==0 then
    self.contentLayer = self:createHeroEquipLayer()
  else
    self.contentLayer = self:createBagTable()
  end
  self.node:addChild(self.contentLayer.node)
  self.currentSelect = index
end
]]
ZBQHLayerFunc.onQiangHuaButton = function(self, node, event)
  NetworkRequest.equipStrengthen(self.currentEquipIndex, "onQiangHua", self)
  --setTouchEnabledAll(ZhuangBeiScene.instance.node, false)
  LoadScene:showLoadingScene()
end

ZBQHLayerFunc.showShineAction = function(self)
  CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile("shine.plist")
  local sp = CCSprite:createWithSpriteFrameName("shine0001.png")
  sp:setPosition(self.curEquipSprite:getPosition())
  self.node:addChild(sp)
  local ac = ActionUtil.createAction("shine.plist",0.02,"")
  sp:runAction(ActionUtil.createSeqAction({ac, CCCallFuncN:create(onRemoveFromParent)}))
end

ZBQHLayerFunc.onQiangHua = function(self, data)
  print("ZBQHLayerFunc.onQiangHua", res)
  local res = cjson.decode(data)
  if res.ret==0 and res.data.result==true then
    NetworkRequest.syncData(res)
    showMessage("强化成功", ccc3(0,255,0))
    self:setEquipInfo(self.currentEquipIndex)
    self:showShineAction()
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
  setTouchEnabledAll(ZhuangBeiScene.instance.node, true)
  LoadScene:removeLoadingScene()
end

ZBQHLayerFunc.onClickEquip = function(self, node, event)
  print(self, node, node:getTag())
  if node:getTag()<100000 then
    self:setEquipInfo(node:getTag())
  end
  --local equipIDs = UserData.userinfo.host.equips
  --local einfo = UserData.config.models.Equip[equipIDs[tostring(node:getTag())].e] 
  
  --if self.curEquipSprite~=nil then
  --  self.curEquipSprite:getParent():removeChild(self.curEquipSprite, true)
  --end
  --self.curEquipSprite = addSpriteFromBK(self.node, ZBQHLayer.NodeID.EquipBK, nameFromPath(einfo.asset))
  --self.currentEquipIndex = node:getTag()
end
--[[
ZBQHLayerFunc.onSelectItem = function(self, node, event)
  if node==self.item1 then
    self:setMenuFocus(0)
  else
    self:setMenuFocus(1)
  end   
end]]
